home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CommToolbox (modified) / Sources / CThread.cp < prev    next >
Encoding:
Text File  |  1994-11-30  |  6.4 KB  |  359 lines  |  [TEXT/KAHL]

  1. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
  2.  
  3.     CThread.cp
  4.     
  5.     Thread Manager class.
  6.     
  7.     SUPERCLASS = CObject.
  8.     
  9.     Copyright © 1994-95 Ithran Einhorn. All rights reserved.
  10.     
  11. ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  12.  
  13. #include <CCluster.h>
  14. #include <CError.h>
  15. #include <Constants.h>
  16. #include <TBUtilities.h>
  17. #include <TCLUtilities.h>
  18.  
  19. #include "CThread.h"                    /* Other includes */
  20.  
  21. #include "string.h"
  22.  
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26.  
  27. #include "ThreadUtil.h"
  28.  
  29. #ifdef __cplusplus
  30. }
  31. #endif
  32.  
  33. /* Constants & Macros */
  34.  
  35. #define RESET_ALRT_ID        2100    /* Reset alert resource ID */
  36.  
  37. /* Application globals */
  38.  
  39. /* Class variables initialization */
  40.  
  41. CCluster    *CThread::cThreadList = NULL;
  42. long        CThread::threadCount;
  43.  
  44. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  45.  
  46. /*
  47.  * cIsPresent
  48.  *
  49.  * Return TRUE if the Thread Manager is present.
  50.  *
  51.  */
  52.  
  53. Boolean CThread::cIsPresent(void)
  54. {
  55.     Boolean    isHere = false;
  56.     long    res;
  57.     short    comparebit;
  58.     OSErr    threadErr;
  59.     
  60.     threadErr = Gestalt (gestaltQuickdrawVersion, &res);
  61.     if (threadErr == noErr)
  62.     {    
  63.         threadErr = Gestalt (gestaltThreadMgrAttr, &res);
  64.         if (threadErr == noErr)
  65.         {
  66.             comparebit = gestaltThreadMgrPresent;
  67.     
  68.             if (BitTst(&res, 31 - comparebit) == TRUE)
  69.                 isHere = true;
  70.         }
  71.     }
  72.  
  73.     return isHere;
  74. }
  75.  
  76. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  77.  
  78. /*
  79.  * cInitManager
  80.  *
  81.  * Thread Manager Initialization
  82.  *
  83.  */
  84.  
  85. void CThread::cInitManager(void)
  86. {
  87.     InitializeThreadUtilities();
  88. }    
  89.  
  90. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  91.  
  92. /*
  93.  * cGetTMVersion
  94.  *
  95.  * return the version of the Thread Manager
  96.  *
  97.  */
  98.  
  99. short CThread::cGetTMVersion(void)
  100. {
  101.     return 0;
  102. }    
  103.  
  104. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  105.  
  106. /*
  107.  * ThreadObjectFromID
  108.  *
  109.  * find CThread object that has given ID
  110.  *
  111.  */
  112.  
  113.          /* Test routine */
  114.  
  115.         static Boolean FindThread(CThread *theThread,ThreadID threadID)
  116.         {
  117.             return theThread->GetID() == threadID;
  118.         }
  119.  
  120.  
  121. CThread *CThread::ThreadObjectFromID(ThreadID threadID)
  122. {
  123.     if (cThreadList == NULL)
  124.         return nil;
  125.     else
  126.     {
  127.         return (CThread *)cThreadList->FindItem1((TestFunc1)FindThread, (long)threadID);
  128.     }
  129. }
  130.  
  131. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  132.  
  133. /*
  134.  * YieldToThread
  135.  *
  136.  * Try to yield to specified Thread object
  137.  *
  138.  *
  139.  */
  140.  
  141. OSErr CThread::YieldToThread (CThread *suggestedThread)
  142. {
  143.  return ::YieldToThread(suggestedThread->GetID());
  144. }
  145.  
  146. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  147.  
  148. /*
  149.  * GetCurrentThread
  150.  *
  151.  * Try to get the current Thread object
  152.  *
  153.  *
  154.  */
  155.  
  156. CThread *CThread::GetCurrentThread (void)
  157. {
  158.  CThread    *currThread = nil;
  159.  ThreadID    currentThreadID;
  160.  
  161.  if (::GetCurrentThread (¤tThreadID) == noErr)
  162.  {
  163.     currThread = ThreadObjectFromID(currentThreadID);
  164.  }
  165.  
  166.  return currThread;
  167. }
  168.  
  169. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  170.  
  171. /*
  172.  * IThread
  173.  *
  174.  * Initialization of the thread object
  175.  *
  176.  *
  177.  */
  178.  
  179. void CThread::IThread(ThreadStyle threadStyle, ThreadEntryProcPtr callBackFunction, CObject *threadedObject, Size stackSize)
  180. {
  181.     short    currentCount = 0;
  182.  
  183.     isActive = false;
  184.     itsThreadStyle = threadStyle;
  185.     itsObject = threadedObject;
  186.  
  187.     lastThreadErr = noErr;
  188.     
  189.     if (cThreadList == NULL)    // make sure we're all initialized
  190.         cInitManager();
  191.  
  192.     lastThreadErr = GetFreeThreadCount (itsThreadStyle, ¤tCount);
  193.  
  194.     if (lastThreadErr == noErr && ! currentCount)
  195.     {
  196.         lastThreadErr = CreateThreadPool (itsThreadStyle, kPoolSegment, (Size)0);
  197.     }
  198.  
  199.     if (lastThreadErr == noErr)
  200.     {
  201.         Lock(true);        // we're using instance variables, so be careful.
  202.  
  203.         lastThreadErr = NewThread (itsThreadStyle, 
  204.                                     callBackFunction,             // callback function
  205.                                     (void *)threadedObject,        // parameter to callback function
  206.                                     stackSize,
  207.                                      kUsePremadeThread,
  208.                                      nil,
  209.                                      &itsThreadID);
  210.  
  211.         Lock(false);
  212.         
  213.         if (lastThreadErr == noErr)
  214.         {
  215.             // default name
  216.             strcpy((char *)threadName,class_name(threadedObject));
  217.     
  218.             if (cThreadList == NULL)            /* first thread object ? */
  219.             {            
  220.                 cThreadList = new(CCluster);
  221.                 cThreadList->ICluster();
  222.  
  223.                 threadCount = 0L;
  224.             }
  225.         
  226.             cThreadList->Add(this);                /* add thread to list */
  227.             threadCount++;
  228.             isActive = true;
  229.         }
  230.     }
  231. }
  232.  
  233. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  234.  
  235. /*
  236.  * Dispose
  237.  *
  238.  * Dispose of a thread object
  239.  *
  240.  */
  241.  
  242. void CThread::Dispose(void)
  243. {
  244.     ASSERT(cThreadList != NULL);
  245.     
  246.     threadCount--;
  247.     cThreadList->Remove(this);            /* Dispose of the thread */
  248.     
  249.     if (cThreadList->IsEmpty())
  250.         ForgetObject(cThreadList);        /* Dispose of the cluster */
  251.  
  252.     DisposeThread (itsThreadID, 0L, TRUE);
  253.     
  254.     inherited::Dispose();                /* Pass message to its superclass */
  255. }
  256.  
  257. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  258.  
  259. /*
  260.  * GetID
  261.  *
  262.  * get thread object's ID
  263.  *
  264.  */
  265.  
  266. ThreadID CThread::GetID(void)
  267. {
  268.     return itsThreadID;
  269. }
  270.  
  271. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  272.  
  273. /*
  274.  * SetName
  275.  *
  276.  * Set the name for this thread object
  277.  *
  278.  */
  279.  
  280. void CThread::SetName(Str32 name)
  281. {
  282.     name[sizeof(threadName) - 1] = '\0';
  283.     
  284.     strcpy((char *)threadName, (char *)name);
  285. }
  286.  
  287. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  288.  
  289. /*
  290.  * LastError
  291.  *
  292.  * get last error
  293.  *
  294.  */
  295.  
  296. OSErr CThread::LastError(void)
  297. {
  298.     return lastThreadErr;
  299. }
  300.  
  301. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  302.  
  303. /*
  304.  * IsActive
  305.  *
  306.  * return thread state
  307.  *
  308.  */
  309.  
  310. Boolean CThread::IsActive(void)
  311. {
  312.     return isActive;
  313. }
  314.  
  315. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  316.  
  317. /*
  318.  * CurrentStackSpace
  319.  *
  320.  * 
  321.  *
  322.  */
  323.  
  324. OSErr CThread::CurrentStackSpace(unsigned long *freeStack)
  325. {
  326.  return ThreadCurrentStackSpace (itsThreadID,freeStack);
  327. }
  328.  
  329. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  330.  
  331. /*
  332.  * GetThreadState
  333.  *
  334.  * 
  335.  *
  336.  */
  337.  
  338. OSErr CThread::GetThreadState(ThreadState *threadState)
  339. {
  340.  return ::GetThreadState (itsThreadID, threadState);
  341. }
  342.  
  343. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  344.  
  345. /*
  346.  * SetThreadState
  347.  *
  348.  * 
  349.  *
  350.  */
  351.  
  352. OSErr CThread::SetThreadState(ThreadState newState,CThread *suggestedThread)
  353. {
  354.  return ::SetThreadState (itsThreadID, newState, suggestedThread->GetID());
  355. }
  356.  
  357. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  358.  
  359.